import requests
import re
from selenium import webdriver
import openpyxl
from selenium.webdriver.chrome.options import Options
import xlrd
import threadpool
import pandas

class Jim():
    def __init__(self):
        self.list_all = []
        self.shuru1 = input('请输入保存的文件名称:')
        self.shuru2 = input('请输入过滤的文件名称,文件后缀名必须为xlsx,多个文件需用+连接:')
        self.filter = self.guolv()
        self.wb = openpyxl.Workbook()
        self.ws = self.wb.create_sheet('sheet1')
        self.chrome_options = Options()
        self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9221")
        self.browser = webdriver.Chrome(options=self.chrome_options,
                                        executable_path='C:\Program Files\Google\Chrome\Application\chromedriver.exe')
        self.list1 = []
        self.list2 = []
        self.cookie = [item["name"] + "=" + item["value"] for item in self.browser.get_cookies()]
        # print(self.cookie)
        self.cookies = str(self.cookie).replace("', '", ";")
        self.cookies1 = re.findall("'(.*?)'", self.cookies)[0]
        print(self.cookies1)

        # self.k = 1
        self.headers = {
            'accept-language': 'en-US,en;q=0.9,zh;q=0.8',
            'cache-control': 'no-cache',
            'cookie': f'{self.cookies1}',
            'pragma': 'no-cache',
            'sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
            'sec-ch-ua-arch': '"x86"',
            'sec-ch-ua-full-version': '"92.0.4515.107"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-model': '""',
            'sec-ch-ua-platform': '"Windows"',
            'sec-ch-ua-platform-version': '"10.0"',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-user': '?1',
            'service-worker-navigation-preload': 'true',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
            'x-client-data': 'CKa1yQEIiLbJAQiktskBCMS2yQEIqZ3KAQi1+soBCNCaywEI6/LLAQjv8ssBCLP4ywEInvnLAQiv+ssBCL7+ywEInv/LAQjrg8wBCNGEzAEYjp7LAQ==',
        }


        self.list3 = []

    def shubiaogundong(self):

        all_xing = self.browser.find_elements_by_xpath(
            '//ytd-video-renderer[@class="style-scope ytd-item-section-renderer"]')
        th = threadpool.ThreadPool(10)
        th_make = threadpool.makeRequests(self.theod, all_xing)
        for th_ in th_make:
            th.putRequest(th_)
        th.wait()
        return self.list_all

    def theod(self, words):

        # for i in all_xing:
        names = words.find_element_by_xpath(
            "./div[@class='style-scope ytd-video-renderer']/div[@class='text-wrapper style-scope ytd-video-renderer']/div[@id='channel-info']/ytd-channel-name[@id='channel-name']/div[@id='container']/div[@id='text-container']/yt-formatted-string[@id='text']").text
        link = words.find_element_by_link_text(f"{names}").get_attribute('href')

        if names not in self.list2 and names not in self.filter:
            list_one = []
            data = ''
            # print(self.k)
            self.list2.append(names)
            self.list1.append(link)
            list_one.append(names)
            list_one.append(link)
            print('*' * 10, link)
            try:
                datas1 = self.req_home(link)
                list_one.extend(datas1)
            except Exception as g:

                print('subscribers请求报错')
                data = '1'

            try:
                videos = link + '/videos'
                data_two = self.req_video(videos)
                list_one.extend(data_two)
            except Exception as g:
                print('videos请求报错')
                data = '1'
            try:
                about = link + '/about'
                data_three = self.req_about(about)
                list_one.extend(data_three)
                print('*' * 20)
            except Exception as g:
                print('about请求报错')
                data = '1'

            if data !='1':

                self.list_all.append(list_one)

        else:
            print('该{}已获取'.format(names))


    def req_home(self, home):
        home_mangen = requests.get(home, headers=self.headers, verify=False).content.decode()
        # print(home_mangen)
        homes = ''
        try:
            homes = re.findall('subscriberCountText(.*?)subscribers', home_mangen)[-1].replace(
                '":{"accessibility":{"accessibilityData":{"label":"', '') + 'subscribers'
        except Exception as g:
            homes = 'subscribers暂无'
        return [homes]


    def req_video(self, videos):
        videos_mangen = requests.get(videos, headers=self.headers, verify=False).content.decode()
        # print(videos_mangen)
        videoss = ''
        times = ''
        try:
            videoss = re.findall('shortViewCountText(.*?)view', videos_mangen)[0].replace(
                '":{"accessibility":{"accessibilityData":{"label":"', '') + 'videos'
            if len(videoss) < 20:
                print(videoss)
                # self.ws.cell(row=self.k, column=3).value = f'{videoss}'
            else:
                videoss = re.findall('shortViewCountText(.*?)watching', videos_mangen)[0].replace(
                    '":{"runs":[{"text":"', '').replace('"},{"text":"', '') + 'videos'
                print(videoss)
                # self.ws.cell(row=self.k, column=3).value = f'{videoss}'
        except Exception as g:
            videoss = 'videoss暂无'
        try:

            times = re.findall('publishedTimeText(.*?)"}', videos_mangen)[0].replace('":{"simpleText":"', '')
            print(times)
        except Exception as f:
            times = 'times暂无'
        # self.ws.cell(row=self.k, column=4).value = f'{times}'
        return [videoss,times]

    def req_about(self, about):
        about_mangen = requests.get(about, headers=self.headers, verify=False).content.decode()
        # print(about_mangen)
        facebook_instagram = ''
        forbus = ''
        addr2 = ''
        instagram = ''
        try:
            all = re.findall('"title":{"simpleText"(.*?)"nofollow":true}}', about_mangen)
            # print(all)
            for i in all:
                facebook_instagram1 = re.findall('"url":"(.*?)","webPageType', i)[0].replace('\\u0026', '&')
                if 'www.facebook' in i:

                    if 'www.facebook' in facebook_instagram1:
                        facebook_instagram = facebook_instagram1
                        print(facebook_instagram)
                        print('*' * 10)
                        # self.ws.cell(row=self.k, column=6).value = f'{facebook_instagram}'
                elif 'www.instagram' in i:
                    if 'www.instagram' in facebook_instagram1:
                        instagram = facebook_instagram1
                        print(instagram)
                        print('*#' * 10)

                        # self.ws.cell(row=self.k, column=7).value = f'{facebook_instagram}'
        except Exception as g:
            facebook_instagram = '暂无facebook或instagram'
            print(facebook_instagram)

        try:
            re.findall('businessEmailButton(.*?)"}', about_mangen)[0].replace('"text":{"runs":[{"text":"', '')

            forbus = '邮箱存在'
            print(forbus)
            # self.ws.cell(row=self.k, column=8).value = f'邮箱存在'

        except Exception as g:

            forbus = '暂无邮箱'
            print(forbus)
            # self.ws.cell(row=self.k, column=8).value = f'暂无邮箱'

        try:

            addr1 = re.findall('thumbnails(.*?)"},"showDescription', about_mangen)[0]
            # print(addr1)
            addr21 = re.findall('country(.*)?', addr1)[0].replace('":{"simpleText":"', '')
            if len(addr21) < 30:
                addr2 = addr21
                print('addr2', addr2)

                # self.ws.cell(row=self.k, column=5).value = f'{addr2}'
            else:
                addr22 = re.findall('country(.*)', addr1)[0].replace('":{"simpleText":"', '').split('"country')[1]

                addr2 = addr22
                print('addr22', addr2)
                # self.ws.cell(row=self.k, column=5).value = f'{addr22}'
        except Exception as g:
            addr2 = '暂无地址'
            print(addr2)

            # self.ws.cell(row=self.k, column=5).value = f'暂无地址'
        return [facebook_instagram,instagram,forbus,addr2]

    def guolv(self):
        list_ = []
        for name in self.shuru2.split('+'):
            workbook = xlrd.open_workbook(r'D:/{}.xlsx'.format(name))
            sheet = workbook.sheet_by_index(0)
            names00 = sheet.col_values(colx=0)
            list_.extend(names00)
        # for i in names00:

        # if i == '':
        #     name00 = 'none'
        # else:
        #     name00 = re.findall("text:'(.*?)'", str(i))[0]
        # print(name00)
        # self.list3.append(name00)
        # print(names00)
        return list_

    def pandas_excel(self):
        df = pandas.DataFrame(self.shubiaogundong(), columns=['名称','链接','subscribers', '观看人数', '时间', '脸书',
                                                         'ins', '邮箱', '地址'])
        df.to_excel(r'D:/{}.xlsx'.format(self.shuru1), index=None)
        print('数据获取完毕！！！！！！')

if __name__ == '__main__':
    run = Jim()
    run.pandas_excel()
    # run.guolv()
# HSID=A0a91_UBxvvQmecuo; SSID=AHRpRkpWgUpNynCoS; APISID=cxsDX4tgleJ1wt_Y/AWPM_dW9wtuivbzyR; SAPISID=-iR396os91RsEr0O/AWj6lfWR-c46dXXNy; __Secure-1PAPISID=-iR396os91RsEr0O/AWj6lfWR-c46dXXNy; __Secure-3PAPISID=-iR396os91RsEr0O/AWj6lfWR-c46dXXNy; LOGIN_INFO=AFmmF2swRQIhANUsyONhWQBXBFN4wgr_xzSOIvMFzc9Eb4YXe3HDVSYPAiBgSNTuIPtPDlmGXSj7ShCqhdZRudDpLsXs5idojTMQbw:QUQ3MjNmejg4X0ZVYU9NQW81NVJDZjY4aVVwcnF0MEFHN21mR0Y0NVhFVzgyTUFWUnhxX0huTWNHQVp4UWFwSmo1NVIyTWFZdzR0TE52NjVFYThEX19sTW5rTlV2UjNnVVpGekE3MmlHYnNJcVlfTnltd0tzSzMzZHRnREFGaXhoOHRwdmJ1TDZYenlpMnZqOHNUd0VzRjFzWVpvVW55Rk5R; VISITOR_INFO1_LIVE=omN03E18CE4; PREF=tz=Asia.Shanghai; SID=CAjoN3IVXRfM9fLXrN0f9BUcmgMTmzOLloMXOfFO4yJvvCg4uuf70WJ1xJhgYOkYCwfFlA.; __Secure-1PSID=CAjoN3IVXRfM9fLXrN0f9BUcmgMTmzOLloMXOfFO4yJvvCg4fIwHBiQxxX0NgBkb3vEZtw.; __Secure-3PSID=CAjoN3IVXRfM9fLXrN0f9BUcmgMTmzOLloMXOfFO4yJvvCg4Mw2S2eFB5nAhWS9nvsknvQ.; YSC=PAcdu_sSBKc; SIDCC=AJi4QfFZlsN94KGXI2SKBGic3RGG18lVWHYyBnUejV7m3keCmF4CU5GRFWxYrjnipO_ulxelD1A; __Secure-3PSIDCC=AJi4QfEsl-ddh5xAI5dli4kVNMZKd7ei1UMTOVy8BCGH0Ls4M4cenXgCXA1pwUj6QdkggegvhBw
